﻿using System;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using BLL;
using EFCachingProvider;
using EFCachingProvider.Caching;
using Model;

namespace Web.Areas.Manager.Controllers
{
    [NoCache]
    [HandleError]
    [Authorize]
    public partial class IndexController : Controller
    {
        public virtual ActionResult Index()
        {

            //读取权限菜单

            Sys_Controllers_BLL _Sys_Controllers_BLL = new Sys_Controllers_BLL();

            var Menus = _Sys_Controllers_BLL.Get().Where(a => a.Sys_Controllers2 == null && a.Enable && a.Sys_Controllers1.Any(b => b.Sys_ActionsInControllersInRoles.Any(c => c.Sys_Roles.Sys_UsersInRoles.Any(d => d.Sys_Users.UserName.Equals(User.Identity.Name))))).OrderBy(a => a.IntOrder);

            Sys_Parameter_BLL _Sys_Parameter_BLL = new Sys_Parameter_BLL();

            ViewData["Title"] = _Sys_Parameter_BLL.Get("系统主名称");

            ViewData["Title1"] = _Sys_Parameter_BLL.Get("系统副名称");

            ViewData["Theme"] = "redmond";

            return View(Menus);
        }


        public virtual ActionResult Desktop(string url)
        {
            
                Sys_Controllers_BLL _Sys_Controllers_BLL = new Sys_Controllers_BLL();

                var Menus = _Sys_Controllers_BLL.Get().Where(a => a.Sys_Controllers2 == null && a.Enable && a.Sys_Controllers1.Any(b => b.Sys_ActionsInControllersInRoles.Any(c => c.Sys_Roles.Sys_UsersInRoles.Any(d => d.Sys_Users.UserName.Equals(User.Identity.Name))))).OrderBy(a => a.IntOrder);

                return View(Menus);
           
        }

        public ActionResult Theme()
        {
            string myPath = Server.MapPath("~/App_Themes/");
            DirectoryInfo TheFolder = new DirectoryInfo(myPath);
            System.IO.DirectoryInfo[] model = TheFolder.GetDirectories();

            return View(model);
        }

        public ActionResult Password()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Password(ChangePasswordModel item)
        {
            if (item.ConfirmPassword != item.NewPassword)
            {
                ModelState.AddModelError("ConfirmPassword", "两次输入的密码不同！");
            }

            if (!ModelState.IsValid)
            {
                return View();
            }

            Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
            _Sys_Users_BLL.ChangePassword(User.Identity.Name, item);

            ModelState.AddModelError("", "密码修改成功！");

            return View();
        }
        public ActionResult datetimenow()
        {
            return Content(DateTime.Now.ToString());
        }
        public ActionResult SystemState()
        {
            InMemoryCache cache = (InMemoryCache)EFCachingProviderConfiguration.DefaultCache;

            ViewData["CacheHits"] = cache.CacheHits;
            ViewData["CacheItemAdds"] = cache.CacheItemAdds;
            ViewData["CacheItemInvalidations"] = cache.CacheItemInvalidations;
            ViewData["CacheMisses"] = cache.CacheMisses;
            ViewData["Count"] = cache.Count;

            ViewData["shooting"] = string.Format("{0:f}", ((double)cache.CacheHits) / (cache.CacheHits + cache.CacheMisses) * 100);

            //用户数量
            //Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
            //ViewData["sys_users_count"] = _Sys_Users_BLL.Get().Count();

            return View();
        }
    }
}
